
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Import;
import org.codehaus.groovy.grails.commons.ApplicationAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils
import org.codehaus.groovy.grails.web.context.ServletContextHolder


import ar.com.photo_admin.domain.*;
import ar.com.photo_admin.service.*;



class BootStrap {
	
	def eventService
	def eventPhotoService
	
	def servletContext
	ApplicationContext ctx	
	
	String SERVER_URL = "../images/upload/";
	String SERVER_PATH = "images/upload";
	String SERVER_BOOTSTRAP_PATH = "web-app/images/bootstrap-images"
	
    def init = { servletContext ->
		
		ctx = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT);
		
		
		def eventPath = getImagesRootPath()+File.separator+"1";
		new File(eventPath).mkdirs();		
				
		//Creo un evento que contenga las imagenes
		Event event1 = new Event(id:1, number:1,name: "evento autogenerado", description:"Aliquam dignissim elementum est sit amet iaculis. Integer pretium ultricies risus, condimentum molestie odio", eventDate: new Date());				

		def anAlbum = new EventAlbum(name:"AlbumAutogenerado1", description:"Aliquam dignissim elementum est sit amet iaculis. Integer pretium ultricies risus, condimentum molestie odio euismod vitae.", event:event1);
		def anotherAlbum = new EventAlbum(name:"AlbumAutogenerado2", description:"",event:event1);
		
		event1.addToAlbums(anAlbum);
		event1.addToAlbums(anotherAlbum);
		
		//Guarda los albums
		event1 = event1.save(flush:true,failOnError: true);
		
		def anEventPhoto = null;
		
		int numberOfPhotos = 0 ;
		
		new File(SERVER_BOOTSTRAP_PATH).eachFile { file ->
			
			if (file.name.endsWith(".jpg") || file.name.endsWith(".png")){
				anEventPhoto = eventPhotoService.createPhotoTemporally(event1.id,file.getBytes(),file.name)								
				
				// Le agrego todas las fotos				
				anAlbum.addToAlbumPhotos(anEventPhoto)
				//.save(flush:true);
				anAlbum.save(flush:true);
				
				if(numberOfPhotos == 1){
					//Hago cover a la foto!
					anAlbum.setCoverPhoto(anEventPhoto)
					anAlbum.save(flush:true);
					event1.setCoverPhoto(anEventPhoto);
				}				
				
				// Al album 2 le agrego la mitado de las fotos
				if(numberOfPhotos % 2 == 0 ){
					anotherAlbum.addToAlbumPhotos(anEventPhoto)
					anotherAlbum.save(flush:true);
					if(numberOfPhotos == 2){
						//Hago cover a la foto!
						anotherAlbum.setCoverPhoto(anEventPhoto)
						anotherAlbum.save(flush:true);
					}
				}
				
				numberOfPhotos++;					
			}
						
		}
		eventService.confirmChangesEvent(event1.id);
		/*event1 = event1.save(flush:true,failOnError: true);*/
		
		User user = new User(email: "nicolasgnr@gmail.com", username: "nicolasgnr", password: "12345678", profileImageUrl: "", facebookUid: "519564145", useFacebookProfileImage: true)
		user.save(flush:true)
		

		
    }
	
    def destroy = {
		
    }
	
	def getImagesRootPath(){
		return ctx.getResource(SERVER_PATH).getFile().toString() + File.separatorChar;
	}
	
	def getBootstrapImagesRootPath(){
		return ctx.getResource(SERVER_BOOTSTRAP_PATH).getFile().toString() + File.separatorChar;
	}	
	
}
